package com.aricksm.xuan.activaty02;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.TextView;

/**
 * Created by meikai on 16/5/1.
 */
public class ExpandableListActivitySe extends Activity {

    private ExpandableListView expandableListView;
    private MyDemoBaseExpandableListAdapter adapter;


    String[] groupStringArr = {"腾讯", "百度", "阿里爸爸"};
    String[][] childStringArr = {
            {"QQ","微信","QQ浏览器"},
            {"百度搜索", "百度地图","百度外卖"},
            {"淘宝", "支付宝", "天猫商城"}
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        expandableListView = (ExpandableListView) findViewById(R.id.demo_expandable_list_view);


        adapter = new MyDemoBaseExpandableListAdapter();

        expandableListView.setAdapter(adapter);


    }


    class MyDemoBaseExpandableListAdapter extends BaseExpandableListAdapter{

        @Override
        public int getGroupCount() {
            return groupStringArr.length;
        }

        @Override
        public int getChildrenCount(int groupPosition) {
            return childStringArr[groupPosition].length;
        }

        @Override
        public Object getGroup(int groupPosition) {
            return groupStringArr[groupPosition];
        }

        @Override
        public Object getChild(int groupPosition, int childPosition) {
            return childStringArr[groupPosition][childPosition];
        }

        @Override
        public long getGroupId(int groupPosition) {
            return groupPosition * 100;
        }

        @Override
        public long getChildId(int groupPosition, int childPosition) {
            return groupPosition * 100 + childPosition;
        }

        @Override
        public boolean hasStableIds() {
            return false;
        }

        @Override
        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {

            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.expandable_list_item_group, null);

            TextView textView = (TextView) view.findViewById(R.id.tv_group);

            Object data = getGroup(groupPosition);

            textView.setText((String)data);

            if(groupPosition % 2 ==1){
                textView.setTextColor(Color.RED);
            }else{
                textView.setTextColor(Color.GREEN);
            }

            return view;
        }

        @Override
        public View getChildView(int groupPosition, int childPosition,
                                 boolean isLastChild, View convertView, ViewGroup parent) {

            View childView = LayoutInflater.from(parent.getContext()).inflate(R.layout.expandable_list_item_child, null);

            TextView textView = (TextView) childView.findViewById(R.id.tv_child);
            Object childData = getChild(groupPosition, childPosition);
            textView.setText((String)childData);

            if(childPosition> 1)
                textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
            else{
                textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
            }

            return childView;
        }

        @Override
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return false;
        }
    }
}
